package tksTcp

import (
	"net"
	. "tksLog"
)

type serverCallback func(net.Conn, uint32, uint32, []byte)

var serverCmds map[uint32]serverCallback

func init() {
	serverCmds = make(map[uint32]serverCallback)
}

func serverInit(conn net.Conn) {
}

func serverReceive(conn net.Conn, cmd uint32, from uint32, to uint32, data []byte) {
	if serverCmds[cmd] != nil {
		serverCmds[cmd](conn, from, to, data)
	}
}

func serverClose(conn net.Conn) {
	Debug("server close connect from %s\n", conn.RemoteAddr().String())
}

func RegisterServerCmd(cmd uint32, cb serverCallback) {
	_, ok := serverCmds[cmd]
	if ok {
		return
	} else {
		serverCmds[cmd] = cb
	}
}

func NewClient(port string) (*TksTcpConn, error) {
	server := new(TksTcpConn)
	err := server.CreateClient(port, serverInit, serverReceive, serverClose)
	if err != nil {
		return nil, err
	}
	return server, nil
}
